package cn.com.xinjinfu.pdoulcProduct.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.view.LoadStatusBox;

/**
 * 安全保障
 * Created by android-adt on 2016/4/11.
 */
public class SalfControlInfoAct extends BaseActivity {

    protected WebView mWebView;
    RelativeLayout top_layout;
    String myUrl,adress;
    private LoadStatusBox loadbox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.salf_control_info);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        myUrl = bundle.getString("url");
        adress = bundle.getString("adress");
        top_layout = (RelativeLayout) findViewById(R.id.top_layout);
        if (adress.equals("salf")){
            top_layout.setBackgroundResource(R.drawable.img_aqbz);
            TitleManager.showTitleWhite(this, "安全保障", "");
        }else {
            top_layout.setBackgroundResource(R.drawable.img_question);
            TitleManager.showTitleWhite(this, "常见问题", "");
        }
        mWebView = (WebView) findViewById(R.id.baseweb_webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        loadbox = (LoadStatusBox) findViewById(R.id.loadStatusBox);
        loadbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                netCheck();
            }
        });
        loadbox.loading();
    }
    private void netCheck(){
        if (!DataHandler.isNetworkConnected(this)) {
            loadbox.failed();
            ToastManager.show(this, "请检查网络连接");
            return;
        }else {
            initData();
            loadbox.success();
        }
    }
    protected void initData() {
        mWebView.loadUrl(myUrl);
        mWebView.setWebViewClient(new MyWebViewClient());
    }

    // 监听 所有点击的链接，如果拦截到我们需要的，就跳转到相对应的页面。
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url != null) {
                LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = layoutInflater.inflate(R.layout.popwindow_web, null);
                final PopupWindow popupWindow = new PopupWindow(popupView,
                        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                popupWindow.setTouchable(true);
                popupWindow.setFocusable(true);
                popupWindow.setBackgroundDrawable(new BitmapDrawable());
                WebView popupWebview = (WebView) popupView.findViewById(R.id.baseweb_webview);
                popupWebview.loadUrl(url);
                popupWindow.showAsDropDown(top_layout);
                popupWindow.setTouchInterceptor(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == KeyEvent.ACTION_DOWN) {
                            mWebView.loadUrl(myUrl);
                        }
                        if (event.getAction() == KeyEvent.ACTION_UP) {

                        }
                        return false;
                    }
                });
            }
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            view.getSettings().setJavaScriptEnabled(true);
            super.onPageFinished(view, url);
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        mWebView = null;
    }

}


